package com.luoxiang.socialandroid.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.common.bean.Gift;
import com.luoxiang.socialandroid.utils.LogUtils;
import com.luoxiang.socialandroid.utils.MmkvUtil;

import java.util.List;

/**
 * @Description TODO
 * @systemUser Administrator
 * @Author LX
 * @Date 09-14-2022 周三 14:50
 */
public class RvgiftAdapter extends RecyclerView.Adapter<RvgiftAdapter.ViewHolder> {
    private List<Gift> list;
    //先声明一个int成员变量,若为0默认显示第一个
    private int thisPosition = -1;
    private int rowindex = 0;


    public RvgiftAdapter(List<Gift> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_gift, parent, false);
        RvgiftAdapter.ViewHolder holder = new RvgiftAdapter.ViewHolder(view);
        return holder;
    }

    @SuppressLint("ResourceType")
    @Override
    public void onBindViewHolder(@NonNull RvgiftAdapter.ViewHolder holder, int position) {

        Glide.with(MyAppliaction.getContext())
                .load("https://dahuixiong.oss-cn-shenzhen.aliyuncs.com/" + list.get(position).getIcon())
                .into(holder.ic_gift);
        holder.gift_name.setText(list.get(position).getName());
        holder.gift_price.setText(list.get(position).getCoin() + holder.gift_price.getContext().getString(R.string.mi_mi));
        if (thisPosition != -1) {
            if (position == thisPosition) {
//            Log.e("wy", "onBindViewHolder: ");
//            viewHolder.itemView.setBackgroundResource(R.color.yellow_ui);
                holder.itemView.setBackgroundResource(R.drawable.shape_giftselect);
            } else {
                holder.itemView.setBackgroundResource(Color.BLACK);

            }
        }
        holder.liner_gifts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                rowindex = position;
                notifyDataSetChanged();
            }
        });
        if (rowindex == position) {
            holder.liner_gifts.setBackgroundResource(R.drawable.shape_bkliwus);
            MmkvUtil.INSTANCE.encode("giftid",list.get(position).getId());
            LogUtils.i("giftss",list.get(position).getId()+"");
        } else {
            holder.liner_gifts.setBackgroundResource(R.drawable.shape_wuliwus);
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ic_gift;
        TextView gift_name, gift_price;
        LinearLayout liner_gifts;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ic_gift = itemView.findViewById(R.id.ic_gift);
            gift_name = itemView.findViewById(R.id.gift_name);
            gift_price = itemView.findViewById(R.id.gift_price);
            liner_gifts = itemView.findViewById(R.id.liner_gifts);

        }
    }
}
